/**  

* @Title: AdministratorDaoHibernate.java 

* @Package com.gxbadge.dao.impl 

* @Description: TODO(用一句话描述该文件做什么) 

* @author CST

* @version V1.0  

*/ 

package com.gxbadge.dao.impl;

import java.util.List;

import com.gxbadge.dao.ImageDao;
import com.gxbadge.dao.base.BaseHibernateDaoSupport;
import com.gxbadge.model.Article;
import com.gxbadge.model.Image;
import com.gxbadge.model.Navigation;

public class ImageDaoHibernate extends BaseHibernateDaoSupport implements ImageDao {

	/* 
	 * {见接口方法说明}
	 */
	public void delete(Image image) {
		
		this.getHibernateTemplate().delete(image);
		
	}

	/* 
	 * {见接口方法说明}
	 */
	public List<Image> findImageAll() {
		
		List<Image> imageList = this.getHibernateTemplate().find("from Image");
		if(imageList!=null&&imageList.size()>=1){
			return imageList;
		}
		return null;
	}
	
	public List<Image> findImageByArti(Article arti){
		
		
		List<Image> imageList = this.getHibernateTemplate().find("from Image as i where i.arti=?", arti);
		if(imageList!=null&&imageList.size()>=1){
			return imageList;
		}
		return null;
		
	}
	
	public List<Image> findImageAllByPage(int offset, int pageSize){
		
		String hql = "from Image as i where i.deleteFlag=false order by i.uploadDate";
		List<Image> imageList = this.findByPage(hql, offset, pageSize);
		hql = null;
		if(imageList!=null&&imageList.size()>=1){
			
			return imageList;
		}
		return null;
		
		
	}
	
	public List<Image> findImageListByNavListAndPage(List<Navigation> navList, int offset, int pageSize){
		
		Object[] values = new Object[navList.size()];
		for(int i=0;i<navList.size();i++){
			values[i] = navList.get(i);
		}
		String hql_param = "";
		for(int i=0;i<navList.size();i++){
			if(i==navList.size()-1){
				hql_param += "i.nav=?";
			}else{
				hql_param += "i.nav=? or ";
			}
			
		}
		String hql = "from Image as i where (" + hql_param + ") order by i.uploadDate desc";
	
		List<Image> imageList = this.findByPage(hql, values, offset, pageSize);
		
		values = null;
		hql = null;
		hql_param = null;
		if(imageList!=null&&imageList.size()>=1){
			return imageList;
		}
		return null;
		
		
	}
	
	public List<Image> findImageListByPage(String hql, int offset, int pageSize){
		
		List<Image> imageList = this.findByPage(hql, offset, pageSize);
		if(imageList!=null&&imageList.size()>=1){
			return imageList;
		}
		return null;
		
	}
	
	/* 
	 * {见接口方法说明}
	 */
	public Image findImageByImageId(Integer imageId) {
		
		Image image = this.getHibernateTemplate().get(Image.class, imageId);
		if(image!=null){
			return image;
		}
		return null;
	}

	/* 
	 * {见接口方法说明}
	 */
	public void save(Image image) {
		
		this.getHibernateTemplate().save(image);
		
	}

	/* 
	 * {见接口方法说明}
	 */
	public void update(Image image) {
		
		this.getHibernateTemplate().update(image);
		
	}
	
	public List<Image> findImageByNavAndPage(int offset, int pageSize, Navigation nav){
		
		String hql = "from Image as i where i.nav=?";
		List<Image> imageList = this.findByPage(hql, nav, offset, pageSize);
		if(imageList!=null&&imageList.size()>=1){
			return imageList;
		}
		return null;
		
	}
	
	public long find_imageCountByNav(Navigation nav){
		
		String hql = "select count(*) from Image as i where i.nav=?";
		long totalCount = (Long)this.getHibernateTemplate().find(hql, nav).listIterator().next();
		return totalCount;
		
		
		
	}
	public long find_imageAllCount(){
		
		String hql = "select count(*) from Image";
		long totalCount = (Long)this.getHibernateTemplate().find(hql).listIterator().next();
		return totalCount;
		
	}
	
	public long find_imageListCountByNavList(List<Navigation> navList){
		
		
		Object[] values = new Object[navList.size()];
		for(int i=0;i<navList.size();i++){
			values[i] = navList.get(i);
		}
		String hql_param = "";
		for(int i=0;i<navList.size();i++){
			
			if(i==navList.size()-1){
				hql_param += "i.nav=?";
			}else{
				hql_param += "i.nav=? or ";
			}
			
		}
		String hql = "select count(*) from Image as i where (" + hql_param + ")";
		
		long totalCount = (Long)this.getHibernateTemplate().find(hql, values).listIterator().next();
		values = null;
		hql = null;
		hql_param = null;
		
		return totalCount;

	}
	
	/* --------------------------------图片处理----------前台----------------------------------------- */
	
	public List<Image> findImageByNavListAndPage(List<Navigation> navList, int offset, int pageSize){
		
		String hql_param = "";
		Object[] values = new Object[navList.size()];
		int i=0;
		while(i<navList.size()){
			values[i] = navList.get(i);
			if(i==navList.size()-1){
				hql_param += "i.nav=?"; 
			}else{
				hql_param += "i.nav=? or "; 
			}
			i++;
		}
		String hql = "from Image as i where i.deleteFlag=false and (" + hql_param + ") order by i.uploadDate desc";
		
		List<Image> imageList = this.findByPage(hql, values, offset, pageSize);
		values = null;
		hql = null;
		hql_param = null;
		if(imageList!=null&&imageList.size()>=1){
			
			return imageList;
		}
		
		return null;
		
	}
	
	public List<Image> findImageByNavAndPage(Navigation nav, int offset, int pageSize){
		
		String hql = "from Image as i where i.deleteFlag=false and i.nav=? order by i.uploadDate desc";
		
		List<Image> imageList = this.findByPage(hql, nav, offset, pageSize);
		
		if(imageList!=null&&imageList.size()>=1){
			
			return imageList;
		}
		
		return null;
		
		
	}
	
	public long f_find_imageCountByNav(Navigation nav){
		
		String hql = "select count(*) from Image as i where i.deleteFlag=false and i.nav=?";
		long totalCount = (Long)this.getHibernateTemplate().find(hql, nav).listIterator().next();
		return totalCount;

	}
	
}
